using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL
{
    /// <summary>
    /// The CityCodeTableDB class is responsible for interacting with the database to retrieve and store information
    /// about CityCodeTable objects.
    /// </summary>
    public class CityCodeTableDB
    {
        #region "Public Methods"

        /// <summary>
        /// Gets an instance of CityCodeTable from the underlying datasource.
        /// </summary>
        /// <param name="cityCode">The unique CityCode of the CityCodeTable in the database.</param>
        /// <returns>An CityCodeTable when the CityCode was found in the database, or null otherwise.</returns>
        public static CityCodeTable GetItem(Int32 cityCode)
        {
            CityCodeTable myCityCodeTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocCityCodeTableSelectSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@cityCode", cityCode);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myCityCodeTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myCityCodeTable;
            }
        }

 
/// <summary>
/// Returns a list with CityCodeTable objects.
/// </summary>
/// <returns>A generics List with the CityCodeTable objects.</returns>

 
        /// <summary>
        /// Returns a list with CityCodeTable objects.
        /// </summary>
        /// <returns>A generics List with the CityCodeTable objects.</returns>
        public static CityCodeTableList GetList()
        {
            CityCodeTableList tempList = new CityCodeTableList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocCityCodeTableSelectList", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;                  

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }


        public static CityCodeTableList GetList(Int32 DistinctCode, Int16 CountryCode)
        {
            CityCodeTableList tempList = new CityCodeTableList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocCityCodeTableSelectList", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myCommand.Parameters.AddWithValue("@districtCode", DistinctCode);
                    myCommand.Parameters.AddWithValue("@countrycode", CountryCode);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }

        /// <summary>
        /// Saves a CityCodeTable in the database.
        /// </summary>
        /// <param name="myCityCodeTable">The CityCodeTable instance to save.</param>
        /// <returns>The new CityCode if the CityCodeTable is new in the database or the existing CityCode when an item was updated.</returns>
        public static Int16 Save(CityCodeTable myCityCodeTable)
        {
            Int16 result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocCityCodeTableInsertUpdateSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    if (myCityCodeTable.CityCode == -1)
                    {
                        myCommand.Parameters.AddWithValue("@cityCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@cityCode", myCityCodeTable.CityCode);
                    }
                    if (myCityCodeTable.DistrictCode == null)
                    {
                        myCommand.Parameters.AddWithValue("@districtCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@districtCode", myCityCodeTable.DistrictCode);
                    }
                    if (myCityCodeTable.CountryCode == null)
                    {
                        myCommand.Parameters.AddWithValue("@countryCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@countryCode", myCityCodeTable.CountryCode);
                    }
                    if (string.IsNullOrEmpty(myCityCodeTable.CityName))
                    {
                        myCommand.Parameters.AddWithValue("@cityName", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@cityName", myCityCodeTable.CityName);
                    }
                    if (string.IsNullOrEmpty(myCityCodeTable.CityShortName))
                    {
                        myCommand.Parameters.AddWithValue("@cityShortName", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@cityShortName", myCityCodeTable.CityShortName);
                    }
                    if (string.IsNullOrEmpty(myCityCodeTable.CityOtherName))
                    {
                        myCommand.Parameters.AddWithValue("@cityOtherName", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@cityOtherName", myCityCodeTable.CityOtherName);
                    }
                    if (string.IsNullOrEmpty(myCityCodeTable.Remark))
                    {
                        myCommand.Parameters.AddWithValue("@remark", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@remark", myCityCodeTable.Remark);
                    }
                    if (myCityCodeTable.Status == null)
                    {
                        myCommand.Parameters.AddWithValue("@status", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@status", myCityCodeTable.Status);
                    }
                    if (myCityCodeTable.CreatedDateTime == null)
                    {
                        myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@createdDateTime", myCityCodeTable.CreatedDateTime);
                    }
                    if (myCityCodeTable.ModifiedDateTime == null)
                    {
                        myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@modifiedDateTime", myCityCodeTable.ModifiedDateTime);
                    }

                    DbParameter returnValue;
                    returnValue = myCommand.CreateParameter();
                    returnValue.Direction = ParameterDirection.ReturnValue;
                    myCommand.Parameters.Add(returnValue);

                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                    result = Convert.ToInt16(returnValue.Value);
                    myConnection.Close();
                }
            }
            return result;
        }

 
/// <summary>
/// Deletes a CityCodeTable from the database.
/// </summary>
/// <param name="cityCode">The CityCode of the CityCodeTable to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>

        /// <summary>
        /// Deletes a CityCodeTable from the database.
        /// </summary>
        /// <param name="cityCode">The CityCode of the CityCodeTable to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        public static bool Delete(int cityCode)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocCityCodeTableDeleteSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
 

                    myCommand.Parameters.AddWithValue("@cityCode", cityCode);
                    myConnection.Open();
                    result = myCommand.ExecuteNonQuery();
                    myConnection.Close();
                }
            }
            return result > 0;
        }

        #endregion 
 
        /// <summary>
        /// Initializes a new instance of the CityCodeTable class and fills it with the data fom the IDataRecord.
        /// </summary>
        private static CityCodeTable FillDataRecord(IDataRecord myDataRecord)
        {
            CityCodeTable myCityCodeTable = new CityCodeTable();
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CityCode")))
            {
                myCityCodeTable.CityCode = myDataRecord.GetInt32(myDataRecord.GetOrdinal("CityCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("DistrictCode")))
            {
                myCityCodeTable.DistrictCode = myDataRecord.GetInt32(myDataRecord.GetOrdinal("DistrictCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CountryCode")))
            {
                myCityCodeTable.CountryCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("CountryCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CityName")))
            {
                myCityCodeTable.CityName = myDataRecord.GetString(myDataRecord.GetOrdinal("CityName"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CityShortName")))
            {
                myCityCodeTable.CityShortName = myDataRecord.GetString(myDataRecord.GetOrdinal("CityShortName"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CityOtherName")))
            {
                myCityCodeTable.CityOtherName = myDataRecord.GetString(myDataRecord.GetOrdinal("CityOtherName"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Remark")))
            {
                myCityCodeTable.Remark = myDataRecord.GetString(myDataRecord.GetOrdinal("Remark"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
            {
                myCityCodeTable.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
            {
                myCityCodeTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
            {
                myCityCodeTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
            }
            return myCityCodeTable;
        }

}

 } 
 
